package com.cat.dataStructure03;

import java.util.Arrays;

/**
 * @author 曲大人的喵
 * @description https://leetcode.cn/problems/find-if-array-can-be-sorted/
 * @create 2025/11/3 09:20
 * @since JDK17
 */

public class Solution01 {
    public boolean canSortArray(int[] nums) {
        int n = nums.length;
        for (int i = 0, j = 0; j < n; i = j) {
            int x = Integer.bitCount(nums[i]);
            while (j < n && x == Integer.bitCount(nums[j])) {
                j++;
            }
            Arrays.sort(nums, i, j);
        }
        for (int i = 1; i < n; i++) {
            if (nums[i] < nums[i - 1]) {
                return false;
            }
        }

        return true;
    }
}
